/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import model.config.MySQLQuery;
import model.pojo.*;
import model.util.MySQLProviders;

/**
 *
 * @author VuTienIT
 */
public class NhaCungCapDAO {
    
    public static NhaCungCapPOJO getNhaCungCap(int MaNCC)
    {
        NhaCungCapPOJO ncc = new NhaCungCapPOJO();
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        try
        {
            String sql = "Select * From NhaCungCap Where MaNCC="+MaNCC;
            ResultSet rs = helper.executeQuery(sql);
            while(rs.next())
            {
                ncc.setMaNCC(rs.getInt("MaNCC"));
                ncc.setTenNCC(rs.getString("TenNCC"));
                ncc.setMoTa(rs.getString("MoTa"));
                ncc.setDienThoai(rs.getString("DienThoai"));
                ncc.setHinh(rs.getString("HinhNCC"));
            }         
        }
        catch(SQLException ex)
        {
            Logger.getLogger(NhaCungCapPOJO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return ncc;
    }
    
    public static ArrayList<NhaCungCapPOJO> getDanhSach()
    {
        ArrayList<NhaCungCapPOJO> s = new ArrayList<NhaCungCapPOJO>();
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        try
        {
            String sql = "Select * From NhaCungCap Where AnHien=1";
            ResultSet rs = helper.executeQuery(sql);
            while(rs.next())
            {
                NhaCungCapPOJO temp = new NhaCungCapPOJO();
                temp.setMaNCC(rs.getInt("MaNCC"));
                temp.setTenNCC(rs.getString("TenNCC"));
                temp.setMoTa(rs.getString("MoTa"));
                temp.setDienThoai(rs.getString("DienThoai"));
                temp.setHinh(rs.getString("HinhNCC"));
                s.add(temp);
            }         
        }
        catch(SQLException ex)
        {
            Logger.getLogger(NhaCungCapPOJO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return s;
    }
    
    
    public static ArrayList<NhaCungCapPOJO> getDanhSachCoPhanTrang(int trang) throws FileNotFoundException, IOException {
      ArrayList<NhaCungCapPOJO> ds = new ArrayList<NhaCungCapPOJO>(); 
      int soSanPhamTrenTrang = ThamSoDAO.laySoLuongTrenTrang();
      int batDau = (trang - 1) * soSanPhamTrenTrang;
      if(soSanPhamTrenTrang==batDau)
          batDau=1;     
      //mo ket noi
      MySQLProviders helper = new MySQLProviders();
      helper.open();
      
      //doc query tu file text
      //lay cau truy van tu file text
     MySQLQuery mySQLQuery=new MySQLQuery();
     mySQLQuery.Read("PHANTRANG");
     String sql=String.format(mySQLQuery.GetQuery(MySQLQuery.QueryKey.SELECT_ALL_NCC_PHANTRANG),batDau,soSanPhamTrenTrang);
      ResultSet rs = helper.executeQuery(sql);
      try {
         while (rs.next()) {
            NhaCungCapPOJO temp = new NhaCungCapPOJO();
            temp.setMaNCC(rs.getInt("MaNCC"));
            temp.setTenNCC(rs.getString("TenNCC"));
            temp.setMoTa(rs.getString("MoTa"));
            temp.setDienThoai(rs.getString("DienThoai"));
            temp.setHinh(rs.getString("HinhNCC"));
            ds.add(temp);
         }
      } catch (SQLException ex) {
         System.out.println(ex.getMessage());
      }
      helper.close();
      return ds;
   }
    
     public static NhaCungCapPOJO getById(int id){
       NhaCungCapPOJO obj=new NhaCungCapPOJO();
       MySQLProviders helper = new MySQLProviders();
       helper.open();
       ResultSet rs = helper.executeQuery("select *from nhacungcap where mancc="+id);
       try {
           while (rs.next()) {            
            obj.setMaNCC(rs.getInt("MaNCC"));
            obj.setTenNCC(rs.getString("TenNCC"));
            obj.setMoTa(rs.getString("MoTa"));
            obj.setDienThoai(rs.getString("DienThoai"));
            obj.setHinh(rs.getString("HinhNCC"));
            
         }
       }catch(Exception e){
           
       }finally{
           helper.close();
       }
       return obj;
   }
}
